package com.gxuwz.leave.dao;

import com.gxuwz.leave.entity.Student;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class StuDaoImpl implements StuDao {
    private JdbcTemplate jdbcTemplate;
    @Override
    public void insert(Student entity) {
        String sql = "insert into sys_student(stuID, classID, password, stuName, sex, address, stuTel, contact, contactTel) values (?, ?, ?, ?, ?, ?, ?, ?, ?)";
        List<Object> paramList = new ArrayList<>();
        if(entity==null){
            return;
        }else{
            paramList.add(entity.getStuID());
            paramList.add(entity.getClassID());
            paramList.add(entity.getPassword());
            paramList.add(entity.getStuName());
            paramList.add(entity.getSex());
            paramList.add(entity.getAddress());
            paramList.add(entity.getStuTel());
            paramList.add(entity.getContact());
            paramList.add(entity.getContactTel());
        }
        jdbcTemplate.update(sql, paramList.toArray());
    }

    @Override
    public void delete(String stuID) {
        String sql = "delete from sys_student where stuID = ? ";
        if(!"".equals(stuID)){
            jdbcTemplate.update(sql, stuID);
        }
    }

    @Override
    public void update(Student entity) {
        String sql = "update sys_student set ";
        List<Object> paramList = new ArrayList<>();
        if(entity ==null){
            return;
        }else{
            if(entity.getClassID()!=null && !"".equals(entity.getClassID())){
                sql+="classID = ?, ";
                paramList.add(entity.getClassID());
            }
            if(entity.getPassword()!=null && !"".equals(entity.getPassword())){
                sql+="password = ?, ";
                paramList.add(entity.getPassword());
            }
            if(entity.getStuName()!=null && !"".equals(entity.getStuName())){
                sql+="stuName = ?, ";
                paramList.add(entity.getStuName());
            }
            if(entity.getSex()!=null && !"".equals(entity.getSex())){
                sql+="sex = ?, ";
                paramList.add(entity.getSex());
            }
            if(entity.getAddress()!=null && !"".equals(entity.getAddress())){
                sql+="address = ?, ";
                paramList.add(entity.getAddress());
            }
            if(entity.getStuTel()!=null && !"".equals(entity.getStuTel())){
                sql+="stuTel = ?, ";
                paramList.add(entity.getStuTel());
            }
            if(entity.getContact()!=null && !"".equals(entity.getContact())){
                sql+="contact = ?, ";
                paramList.add(entity.getContact());
            }
            if(entity.getContactTel()!=null && !"".equals(entity.getContactTel())){
                sql+="contactTel = ?, ";
                paramList.add(entity.getContactTel());
            }
            // 移除多余的逗号和空格
            sql = sql.substring(0, sql.length() - 2);
            sql+="where stuID = ?";
            paramList.add(entity.getStuID());
        }
        jdbcTemplate.update(sql, paramList.toArray());
    }

    @Override
    public List<Student> query(Student entity) {
        String sql = "select * from sys_student where 1=1 ";
        List<Student> studentes = new ArrayList<>();
        List<Object> paramList = new ArrayList<>();
        if(entity!=null){
            if(entity.getStuID()!=null && !"".equals(entity.getStuID())){
                sql+="and stuID = ?";
                paramList.add(entity.getStuID());
            }
            if(entity.getPassword()!=null && !"".equals(entity.getPassword())){
                sql +=" and password = ?";
                paramList.add(entity.getPassword());
            }
            if(entity.getStuName()!=null && !"".equals(entity.getStuName())){
                sql+="and stuName like ?";
                paramList.add("%" + entity.getStuName() + "%");
            }
        }
        studentes = jdbcTemplate.query(sql, new RowMapper<Student>() {
            @Override
            public Student mapRow(ResultSet resultSet, int i) throws SQLException {
                Student student = new Student();
                student.setStuID(resultSet.getString("stuID"));
                student.setClassID(resultSet.getString("classID"));
                student.setPassword(resultSet.getString("password"));
                student.setStuName(resultSet.getString("stuName"));
                student.setSex(resultSet.getString("sex"));
                student.setAddress(resultSet.getString("address"));
                student.setStuTel(resultSet.getString("stuTel"));
                student.setContact(resultSet.getString("contact"));
                student.setContactTel(resultSet.getString("contactTel"));
                return student;
            }
        }, paramList.toArray());
        return studentes;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate){
        this.jdbcTemplate = jdbcTemplate;
    }
}
